﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ArgusLib.Controls
{
	public class CharSet
	{
		#region Properties
		public char Lower { get; private set; }
		public char Upper { get; private set; }
		#endregion

		#region Constructors
		private CharSet() { }

		public CharSet(char lower, char upper)
		{
			if (lower <= upper)
			{
				this.Lower = lower;
				this.Upper = upper;
			}
			else
			{
				this.Lower = upper;
				this.Upper = lower;
			}
		}

		public static CharSet[] FromChars(string chars)
		{
			return CharSet.FromChars(chars.ToCharArray());
		}

		public static CharSet[] FromChars(params Char[] chars)
		{
			Array.Sort<char>(chars);
			List<CharSet> RetVal = new List<CharSet>();
			CharSet set = new CharSet();
			set.Lower = chars[0];
			int v0 = (int)chars[0];
			int v1;
			for (int i = 1; i < chars.Length; i++)
			{
				v1 = (int)chars[i];
				if (v1 - v0 > 1)
				{
					set.Upper = chars[i - 1];
					RetVal.Add(set);
					set = new CharSet();
					set.Lower = chars[i];
				}
				v0 = v1;
			}

			set.Upper = chars[chars.Length - 1];
			RetVal.Add(set);
			return RetVal.ToArray();
		}
		#endregion

		#region Public Methods
		public bool Check(char c)
		{
			if (c >= this.Lower && c <= this.Upper)
				return true;
			return false;
		}
		#endregion
	}
}
